<?php

namespace App\Service\Index\v1_2_0;

use App\Cache\UserRemarkCache;
use App\Model\CallBase;
use App\Model\CloseFriend;
use App\Model\Setting\SettingUserLevel;
use App\Model\UserProfile;
use App\Model\VideoCall;
use App\Service\ServiceTrait\GetUserInfoTrait;

/**
 * 视频呼叫页面信息
 */
class VideoCallingInfoService extends CallingBaseService
{
    Use GetUserInfoTrait;

    protected function RoVerifyList()
    {
        return [
            'chat_user_id' => 'required|int',
            'is_caller' => 'required|int',
            'call_from' => 'required|int',
        ];
    }

    public function handle()
    {
        $userId = $this->getUserId();
        $chatUserId = $this->request->input('chat_user_id', 0);
        //是否呼叫方：1-呼叫，0-被叫
        $isCaller = $this->request->input('is_caller', 1);
        $callFrom = $this->request->input('call_from', 0);
        //1-系统呼叫，0-默认
        $fromSys = $this->request->input('from_sys', 0);
        if ($fromSys == 1) return $this->packSysInfo($chatUserId, $userId);
        return $this->packInfo($chatUserId, $userId, $isCaller, $callFrom);
    }

    /**
     * 系统呼叫
     */
    function packSysInfo($chatUserId, $userId)
    {
        $ret = VideoCall::packCallinfInfo($chatUserId);
        $ret['call_tip'] = $this->callTips('video');
        log_user_result($userId, "{$chatUserId}==" . json_encode($ret));
        return $ret;
    }

    /**
     * 呼叫页面信息
     */
    function packInfo($chatUserId = 0, $userId = 0, $isCaller = 1, $callFrom = 0)
    {
        //资费
        $callRecord = VideoCall::checkRecord($userId, $chatUserId);
        if (!$callRecord) return [];
        $coinSetting = CallBase::getCallCoinSetting($callRecord->toArray(), VideoCall::CALL_TYPE);
        //资料
        $profile = UserProfile::getProfile($chatUserId);
        $posters = UserProfile::getPosters($chatUserId, $profile, false);
        //天天福利
        $callRecord['from'] == CallBase::FROM_DAILY_FREE && $callFrom = 2;

        //视频邀约
        $callRecord['from'] == CallBase::FROM_INVITE_CALL && $callFrom = 4;

        if($callFrom == 4)
        {
            $isCaller = $profile->gender;

        }

        //特惠视频
        $callRecord['from'] == CallBase::FROM_DISCOUNT_CALL && $callFrom = 10;

        $coinText = $this->getCoinText(
            $coinSetting,
            $callFrom,
            $isCaller,
            VideoCall::CALL_TYPE,
            $callRecord['from'],
            $callRecord['call_mark']
        );
        $ret = [
            'nick_name' => $profile->nick_name,
            'avatar' => $profile->avatar,
            'gender' => $profile->gender,
            'age' => $profile->age,
            'city' => '',
            'level' => 1, //SettingUserLevel::getUserLevel($profile),
            'close_value' => CloseFriend::getCloseValue($userId, $chatUserId),
            'max_close_value' => 100,
            'call_tip' => $this->callTips('video'),
            'coin_setting' => $coinText,
            'bg_video' => $posters['video'],
            'bg_image' => $posters['photo'] ? $posters['photo'][0] : '',
            'remark' => $userId != $chatUserId ? (new UserRemarkCache())->getRemark($userId, $chatUserId) : '',
        ];
        log_user_result($userId, "{$chatUserId}==" . json_encode($ret));
        return $ret;
    }
}
